﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.FileLayer;

namespace HA.ModFlow.Domain.DomainLayer
{
    [Serializable]
   public class SpecifiedHeadCollecion:BoundaryCollecionBase
    {
        public SpecifiedHeadCollecion(int nrows, int ncolumns, int nlayers)
            : base(nrows, ncolumns, nlayers)
        {

            _innerList = new List<SpecifiedHead>();
 
        }



        List<SpecifiedHead> _innerList;

        public List<SpecifiedHead> InnerList
        {
            get { return _innerList; }

        }


        #region 公共函数

        public override void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
        {
            base._stressPeriods_StressPeriodChanged(sender, e);

            List<StressPeriod> lst = (List<StressPeriod>)e.Param;
            foreach (SpecifiedHead item in this._innerList)
            {
                item.OnResetSperiods(lst.Count);
            }

        }

        public List<SpecifiedHead> GetSpecifiedHead(int index)
        {
            List<SpecifiedHead> heads = new List<SpecifiedHead>();


            foreach (SpecifiedHead item in this._innerList)
            {
                if (item.Index == index)
                {

                    heads.Add(item);

                }


            }

            return heads;
        }

        public void UpdateSpecifiedHeads(List<SpecifiedHead> heads, List<int> indices)
        {
            //取得所有的index元素

            List<SpecifiedHead> orgHeads = new List<SpecifiedHead>();

            foreach (int index in indices)
            {
                orgHeads.AddRange(this.GetSpecifiedHead(index));

            }


            foreach (SpecifiedHead head in orgHeads)
            {
                this._innerList.Remove(head);


            }


            this._innerList.AddRange(heads);


            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.SPECIFIEDHEAD));


 
        }



        /// <summary>
        ///其他地方勿，用将临时存储存入数据中心，并不允许重复ijk,概念映射专用函数，
        /// </summary>
        /// <param name="heads"></param>
        public void ResetSpecifiedHeads(List<SpecifiedHead> heads)
        {
            this._innerList.Clear();

            //foreach (SpecifiedHead item in heads)
            //{
            //    if (!this._innerList.Exists(h=>h.Index==item.Index))
            //    {

            //        this._innerList.Add(item);
                
            //    }


            //}

            this._innerList.AddRange(heads);
           

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.SPECIFIEDHEAD));


            
        }


        public PSpecifiedHead ToSpecifiedHeadPack()
        {

            PSpecifiedHead p = new PSpecifiedHead();

            p.MAXACTC = this._innerList.Count;

           

            for (int i = 0; i < this.NPeriods; i++)
            {
                SPSpecifiedHead sp = new SPSpecifiedHead();


                if (i == 0)
                {
                    sp.ITMP = this._innerList.Count;
                    

                    foreach (SpecifiedHead item in this._innerList)
                    {
                        GPointValue pv = new GPointValue(item.K, item.I, item.J, item.SHead[i], item.EHead[i]);


                        sp.HeadsData.Add(pv);
                    }


                }

                else
                {
                    if (this.IsUsePrecious[i] == true)
                    {
                        sp.ITMP = -1;

                    }
                    else
                    {

                        sp.ITMP = this._innerList.Count;

                        foreach (SpecifiedHead item in this._innerList)
                        {
                            GPointValue pv = new GPointValue(item.K, item.I, item.J, item.SHead[i], item.EHead[i]);


                            sp.HeadsData.Add(pv);
                        }

                    }
                }




                p.PeriodsData.Add(sp);




            }



            return p;
 

        }


        public void Clear()
        {

            this._innerList.Clear();

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.SPECIFIEDHEAD));

 
        }

        public override void OnGridReset(int nrows, int ncols, int nlayers)
        {
            base.OnGridReset(nrows, ncols, nlayers);

            this.Clear();


        }



        #endregion


        List<SpecifiedHead> _tempSpecifiedHeads = new List<SpecifiedHead>();
        bool _haveTempData = false;
        public bool HaveTempData
        {
            get { return _haveTempData; }
        }

        public void AddTempSpecifiedHeads(List<SpecifiedHead> specifiedHead)
        {
            this._tempSpecifiedHeads.AddRange(specifiedHead);
            this._haveTempData = true;
        }


        /// <summary>
        /// 将暂存的数据提供给核心，并清空对应的选项
        /// </summary>
        public void CommitTempSpecifiedHeads()
        {
            if (this._haveTempData)
            {
                this.ResetSpecifiedHeads(this._tempSpecifiedHeads);
                this._tempSpecifiedHeads.Clear();
            }
            this._haveTempData = false;
        }

    }
}
